package com.bawei.library_network.interceptor

import okhttp3.Interceptor
import okhttp3.Response

/**

 * @Author : ytx

 * @Time : On 2023/5/20 08:59

 * @Description : RetryInterceptor 超时重连拦截器设置次数

 */
class RetryInterceptor(val maxRetry:Int):Interceptor {//maxRetry 最大次数
    private var retryNum = 0//已经链接次数
    override fun intercept(chain: Interceptor.Chain): Response {
        val  request = chain.request()
        var response = chain.proceed(request)
        while (!response.isSuccessful && retryNum < maxRetry){//失败并且小于最大
            retryNum++
            response = chain.proceed(request)//再次发起请求
        }
        return response

    }
}